package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.Supplier;
import cn.wolfcode.crm.mapper.SupplierMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class SupplierServiceImpl implements ISupplierService {
    @Autowired
    private SupplierMapper supplierMapper;


    @Override
    public void deleteByPrimaryKey(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUPdate(Supplier supplier) {
        if (supplier.getId()==null) {
            supplier.setInputTime(new Date());
            supplierMapper.insert(supplier);
        }else {
            supplierMapper.updateByPrimaryKey(supplier);
        }
    }

    @Override
    public Supplier selectByPrimaryKey(Long id) {
        return supplierMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Supplier> selectAll() {
        return supplierMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int total = supplierMapper.selectCount(qo);
        if(total==0){
            return new PageResult(total, Collections.EMPTY_LIST);
        }
        List<Supplier> rows = supplierMapper.queryForList(qo);
        return new PageResult(total,rows);
    }
}
